<?php
namespace Home\Controller;

use Think\Controller;

class UserController extends BaseController
{

    /**
     * 登录
     */
    public function login()
    {
        $this->display();
    }

    /**
     * 登录验证
     */
    public function loginAjax()
    {
        $logEmail = trans_p("email");
        $logPsw = trans_p('psw');
        $selectData = D("User")->selectLoginInfo("u_email='$logEmail' and u_password='$logPsw'");
        if ($selectData != null) { // 登录成功
            $_SESSION['Home']['email'] = $logEmail;
            $_SESSION['Home']['sub_email'] = substr_replace($logEmail, "****", 3,4);
            $yhidData = D("User")->selectYhIdInfo("u_email='$logEmail'");
            $_SESSION['Home']['yhid'] = $yhidData['u_yhid'];
            $this->ajaxReturn(array(
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array(
                'status' => 0
            ));
        }
    }

    /**
     * 注册
     */
    public function register()
    {
        $this->display();
    }

    /**
     * 注册验证
     * $reg_data 注册信息
     */
    public function registerAjax()
    {
        $regData = array();
        $regData['u_email'] = trans_p("regEmail");
        $regData['u_password'] = trans_p('regPsw');
        $regData['u_registerTime'] = date("Y-m-d H:i:s", time()); // 注册时间
        $regData['u_role'] = trans_p("regRole");
        
        $nextId = D("User")->getNextAutoIncrementId();
        $regData['u_yhId'] = "YH" . date("ymd", time()) . num_format($nextId); // 用户编号
        
        $insertData = D("User")->insertIntoUser($regData);
        if ($insertData != null) { // 注册成功
            $_SESSION['Home']['reg_email'] = $regData['u_email'];
            $this->ajaxReturn(array(
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array(
                'status' => 0
            ));
        }
    }

    /**
     * 修改密码
     * $findPswData 修改密码信息
     */
    public function findPswAjax()
    {
        $findPswData = array();
        $findPswData['u_password'] = trans_p('logPsw');
        $email = trans_p("email");

        $modData = D("User")->modPassword($findPswData, "u_email='$email'");
        if ($modData === false) {
            $this->ajaxReturn(array(
                'status' => 0
            ));
            return false;
        }
        $this->ajaxReturn(array(
            'status' => 1
        ));
        return false;
    }

    /**
     * 图片验证码配置
     */
    public function identifyImg()
    {
        ob_clean(); // 丢弃输出缓冲区中的内容
        
        $config = array(
            'fontSize' => 14, // 字体大小
            'length' => 4, // 验证码位数
            'useCurve' => true, // 是否画混淆曲线
            'useNoise' => true, // 开关验证码杂点
            'imageW' => 100, // 图片宽
            'imageH' => 43
        ); // 图片高
        
        $Verify = new \Think\Verify($config);
        
        $Verify->codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
        
        $Verify->entry();
    }

    /**
     * 判断验证码是否正确
     */
    public function checkIdentify()
    {
        $identify = trans_p("identify");
        $checkIdentify = check_identify($identify);
        if ($checkIdentify == true) {
            $this->ajaxReturn(array(
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array(
                'status' => 0
            ));
        }
    }

    /**
     * 邮箱号唯一
     * $mailNum 邮箱号
     */
    public function emailUnique()
    {
        $mailNum = trans_p('mailNumber');
        $data = D("User")->judgeEmailUnique("u_email='$mailNum'", "u_email");
        if ($data) {
            $this->ajaxReturn(array( // 已存在
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array( // 不存在
                'status' => 0
            ));
        }
    }

    /**
     * 发送验证码
     * $identify 自动生成的验证码
     * $mailNumber 邮箱号
     * $mailCode 前台邮箱验证码
     * $subject 标题
     * $content 邮件内容
     */
    public function sendCode()
    {
        for ($i = 1; $i < 5; $i ++) {
            $identify .= rand(0, 9);
        }
        $date = date("Y年m月d日 H时i分s秒", time());
        $_SESSION['Home']['code'] = $identify;
        if (! isset($_SESSION['Home']['time'])) {
            $_SESSION['Home']['time'] = time();
        }
        $mailNumber = trans_g('mailNum');
        $subject = "尊敬的用户";
        $content = "亲爱的用户,您于$date 申请验证邮箱,验证码为$identify,有效期2分钟。<br/>
          若非本人操作,请忽略此邮件。<p>优得网<hr/>
          注：此邮件由系统自动发送,请勿回复。";

        $data = sendMail($mailNumber, $subject, $content); // 向邮箱发送生成的验证码
        if ($data) {
            $this->ajaxReturn(array( // 发送成功
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array(
                'status' => 0
            ));
        }
    }

    /**
     * 判断验证码是否一致
     * $mailCode 输入的验证码
     */
    public function judgeCode()
    {
        $mailCode = trans_g("mailCode");
        if (($_SESSION['Home']['time'] + 120) < time()) {
            unset($_SESSION['Home']['code']);
            unset($_SESSION['Home']['time']); // 过期销毁
            $this->ajaxReturn(array( // 验证码失效
                'status' => 2
            ));
        } else {
            if ($_SESSION['Home']['code'] == $mailCode) { // 验证码一致
                $this->ajaxReturn(array(
                    'status' => 1
                ));
            } else {
                $this->ajaxReturn(array( // 验证码不一致
                    'status' => 0
                ));
            }
        }
    }

    /**
     * 退出
     */
    public function exitUser()
    {
        unset($_SESSION['Home']);
    }
}